package com.pureut.system.service.impl;

import com.pureut.common.core.utils.StringUtils;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.Options;
import com.pureut.system.domain.PrintDictionary;
import com.pureut.system.domain.SysPrintTemplate;
import com.pureut.system.domain.SysTagSummary;
import com.pureut.system.domain.dto.SysPrintTemplateDto;
import com.pureut.system.domain.dto.SysTagSummaryDto;
import com.pureut.system.domain.vo.SysPrintTemplateVo;
import com.pureut.system.mapper.SysPrintTemplateMapper;
import com.pureut.system.service.ISysPrintTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 打印模板实现类
 * @Date 23-03-15
 * @Author hl
 **/
@Service
public class SysPrintTemplateServiceImpl implements ISysPrintTemplateService {

    @Resource
    private SysPrintTemplateMapper sysPrintTemplateMapper;

    /**
     * 打印模板列表
     * @param sysPrintTemplateVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<SysPrintTemplateDto> selectPrintTemplateList(SysPrintTemplateVo sysPrintTemplateVo) {
        String routing = sysPrintTemplateVo.getPrintRouting();
        if(!StringUtils.isEmpty(routing)){
            sysPrintTemplateVo.setPrintRouting(routing.substring(1)+"/index");
        }
        List<SysPrintTemplateDto> list = sysPrintTemplateMapper.selectPrintTemplateList(sysPrintTemplateVo);
        List<SysDictData> netArray = DictUtils.getDictCache("sys_print_type");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        for (SysPrintTemplateDto dto:
        list) {
            dto.setPrintTypeDict(netMap.get(String.valueOf(dto.getPrintType())));
        }
        return list;
    }

    /**
     * 新增模板列表
     * @param sysPrintTemplate
     * @return
     */
    @Override
    public int insertPrintTemplate(SysPrintTemplate sysPrintTemplate) {
        sysPrintTemplate.setDeptId(SecurityUtils.getDeptId());
        sysPrintTemplate.setCreateBy(SecurityUtils.getUsername());
        sysPrintTemplate.setPrintCode("测试");
        return sysPrintTemplateMapper.insertPrintTemplate(sysPrintTemplate);
    }

    /**
     * 查询单个模板列表
     * @param printId
     * @return SysPrintTemplateDto
     */
    @Override
    public SysPrintTemplateDto selectPrintTemplateById(long printId) {
        SysPrintTemplateVo vo = new SysPrintTemplateVo();
        vo.setPrintId(printId);
        List<SysPrintTemplateDto> list = selectPrintTemplateList(vo);
        return list.get(0);
    }

    /**
     * 批量删除打印模板
     * @param printId
     * @return 1 成功 0 失败
     */
    @Override
    public int deletePrintTemplate(String printId) {
        String[] printIdS = printId.split(",");
        return sysPrintTemplateMapper.deletePrintTemplate(printIdS);
    }

    /**
     * 修改打印模板
     * @param sysPrintTemplate
     * @return 1 成功 0 失败
     */
    @Override
    public int updatePrintTemplateEdit(SysPrintTemplate sysPrintTemplate) {
        return sysPrintTemplateMapper.updatePrintTemplateEdit(sysPrintTemplate);
    }

    /**
     * 返回JSON配置
     * @param printId
     * @return String
     */
    @Override
    public String getPrintTemplateConfig(long printId) {
        return sysPrintTemplateMapper.getPrintTemplateConfig(printId);
    }

    /**
     * 查询字段
     * @param printCorrelation
     * @return SysTagSummary
     */
    @Override
    public List<SysTagSummaryDto> getPrintTemplateDetails(long printCorrelation) {
        List<SysTagSummary> sysList = sysPrintTemplateMapper.getPrintTemplateDetails(printCorrelation);
        List<SysTagSummaryDto> listDto = new ArrayList<>();
        for (SysTagSummary sys:
        sysList) {
            SysTagSummaryDto dto = new SysTagSummaryDto();
            dto.setTid(sys.getTid());
            dto.setData(sys.getData());
            dto.setType(sys.getType());
            dto.setTitle(sys.getTitle());
            dto.setIcon(sys.getIcon());
            Options op = new Options();
            op.setHeight(sys.getHeight());
            op.setField(sys.getField());
            op.setFontSize(sys.getFontSize());
            op.setLineHeight(sys.getLineHeight());
            op.setTestData(sys.getTestData());
            op.setTextType(sys.getTextType());
            dto.setOptions(op);
            listDto.add(dto);
        }
        return listDto;
    }

    /**
     *
     * @param type
     * @return
     */
    @Override
    public List<SysPrintTemplateDto> selectList(int type) {
        return sysPrintTemplateMapper.selectList(type);
    }

    /**
     * 通过模板id新增字段
     */
    @Override
    public int insertPrintDictionary(List<PrintDictionary> list) {
        PrintDictionary pr = list.get(0);
        int ic = sysPrintTemplateMapper.delPrintDictionary(pr.getPrintCorrelation());
        if(ic>0){
            ic = sysPrintTemplateMapper.insertPrintDictionary(list);
        }
        return ic;
    }
}

